<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\Http\Requests\HomePost;
use Auth;
use Illuminate\Support\Facades\DB;

use Illuminate\Support\Facades\Storage;
use Session;
  
class MemberController extends Controller
{
    public function member_center(){
        //dd(Auth::guard('admin')->user());die;获取用户信息
        $uid = Auth::guard('user')->user()->id;//获取用户信息
        //$find = DB::table('admins')->where(1)->get();//获取多条数据
        $find = (array)DB::table('users')->where('id',$uid)->first();//dd($find);die;
        return view('home.member_center',$find);
    }
    public function change_password(){
        $uid = Auth::guard('user')->user()->id;//获取用户信息
        //$find = DB::table('admins')->where(1)->get();//获取多条数据
        $find = DB::table('users')->where('id',$uid)->first();
       // print_r($find->username);die;取值
        return view('home.change_password');
    }
    public function new_pwd(HomePost $request){
        $model = Auth::guard('user')->user();
        $model->password = bcrypt($request['password']);
        DB::beginTransaction();
        try{
            $model->save();
            DB::commit();
            //提示用户flash:https://packagist.org/packages/laracasts/flash 
            flash('密码修改成功！')->overlay();//将消息渲染为叠加层。
            return redirect()->back();
        }catch(\Exception $e){
            return $e->getMessage();
            DB::rollBack();
        }
        
    }
    public function headimg(Request $request){
        if (!$request->hasFile('headimg')) { 
            flash('上传文件为空或者上传文件过大')->overlay();
            return redirect()->back();
        }
        $upload_files = $request->file();
        if(empty($upload_files) || !is_array($upload_files)){
            flash('上传失败!')->overlay();
            return redirect()->back();
        }
        //判断文件上传过程中是否出错//
        $file = $request->file('headimg');
        if(!$file->isValid()){
            flash('文件上传出错！')->overlay();
            return redirect()->back();
        }else{
            //获取原文件名
            $originalname = $file->getClientOriginalName();
            //扩展名
            $ext = $file->getClientOriginalExtension();
            //文件类型
            $type = $file->getClientMimeType();
            $allow_type= [
                'image/jpeg',
                'application/pdf',
                'image/jpg',
                'application/octet-stream',
                'image/png','image/gif',
                'application/msword',
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                'application/vnd.ms-excel',
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            ];
            if(!in_array($type,$allow_type)){
                exit('文件类型不允许！');
            }
            $path = $request->file('headimg')->store('public/header');
            $input = $request->all();
            $input['file_url']  = $path;
            $input['file_name'] = $originalname;
            $filename = 'head'.date('Y-m-d-H-i-s').'.'.$ext;//重命名
            //Storage::disk('upload')->put($filename, file_get_contents($path));//上传
            //$add                = Files::create($input);
            DB::table('users')
            ->where('id', Auth::guard('user')->user()->id)
            ->update(['headimg' => $input['file_url']]);
            flash('上传成功！')->overlay();//将消息渲染为叠加层。
            return redirect()->back();
        }
    }
}
